package com.chuanke.ckfamily.service.social.thread;

import com.chuanke.ckfamily.dao.base.Page;
import com.chuanke.ckfamily.dao.social.model.Family;
import com.chuanke.ckfamily.dao.user.model.User;
import com.chuanke.ckfamily.service.common.SysConfigService;
import com.chuanke.ckfamily.service.constant.FamilyConstance;
import com.chuanke.ckfamily.service.constant.SysConfigKeyEnum;
import com.chuanke.ckfamily.service.exception.CFException;
import com.chuanke.ckfamily.service.social.FamilyMemberService;
import com.chuanke.ckfamily.service.social.FamilyService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.List;

public class LoadFansToFamilyRunable implements Runnable {

    private static final Logger log = LoggerFactory.getLogger(LoadFansToFamilyRunable.class);

    private Long uid;

    private Family family;

    private boolean auto;

    private FamilyMemberService familyMemberService;

    private FamilyService familyService;
    
    private SysConfigService sysConfigService;

    public LoadFansToFamilyRunable(Long uid, Family family, boolean auto, FamilyService familyService, FamilyMemberService familyMemberService, SysConfigService sysConfigService) {
        this.uid = uid;
        this.auto = auto;
        this.family = family;
        this.familyService = familyService;
        this.familyMemberService = familyMemberService;
        this.sysConfigService=sysConfigService;
    }

    @Override
	public void run() {
		try {
			int capacity = sysConfigService.getIntegerValue(SysConfigKeyEnum.create_family_user_num.name(),FamilyConstance.FAMILY_AUTO_SIZE);

			Page page = new Page(0, capacity);
			List<User> fansList = familyMemberService.getCanAddMember(uid,null, page, null);
			log.debug("创建家族 粉丝拉入本家族 ={}, capacity={}, size={}", family.getId(), capacity, fansList.size());

			if (fansList != null && !fansList.isEmpty()) {
				familyMemberService.addFamilyMembers(family, fansList, auto);
			}

		} catch (CFException e) {
			e.printStackTrace();

		}
	}
}
